package com.aplink.generic.widgets;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

import com.aplink.generic.GenericFragment;
import com.aplink.generic.constant.type.TabhostDisplay;
import com.aplink.generic.dictionary.GenericDictionary;
import com.aplink.generic.interfaces.OnGetObjProInfo;
import com.aplink.generic.pojo.GenericAttribute;
import com.aplink.generic.pojo.PageItem;
import com.aplink.generic.pojo.controlspec.ControlSpec;
import com.aplink.generic.pojo.controlspec.tabhost.TabHostSpec;
import com.aplink.generic.xmlparser.ContentDataParser;
import com.aplink.generic.xmlparser.FormDesignParser;
import com.aplink.sg.R;

public class ApLinkTabhost extends ApLinkBaseView {
    /**
     * 
     */
    private static final long serialVersionUID = -3664719345148733469L;
    private final FragmentTabHost mFragmentTabHost;
    private TabHostSpec mTabhostFormat = null;

    public ApLinkTabhost(final GenericFragment genericFragment,
            final ControlSpec controlSpec, final View view) {
        super(genericFragment, controlSpec, view);
        this.mFragmentTabHost = (FragmentTabHost) view;
        this.mFragmentTabHost.setup(getApplicationContext(),
                getChildFragmentManager(), R.id.realtabcontent);
        this.mTabhostFormat = (TabHostSpec) controlSpec;
    }

    @Override
    public void clear() {

    }

    @Override
    public GenericAttribute getAttribute() {
        return null;
    }

    @Override
    public GenericDictionary<String, GenericAttribute> getListAttributes() {
        return null;
    }

    @Override
    public FragmentTabHost getView() {
        return this.mFragmentTabHost;
    }

    // private OnMenuItemClickListener onPopupMenuItemClick = new
    // OnMenuItemClickListener() {
    //
    // @Override
    // public boolean onMenuItemClick(MenuItem item) {
    // Bundle bundle = item.getIntent().getExtras();
    // MenuItemSpec popupMenuItem = (MenuItemSpec) bundle
    // .getSerializable(BundleConstant.POPUP_MENU_ITEM);
    // int tabId = bundle.getInt(BundleConstant.SELECTED_INDEX);
    // mFragmentTabHost.setCurrentTab(tabId);
    // GenericFragment genericFragment = GenericFragment.newInstance(
    // popupMenuItem.getFormDesignID(),
    // popupMenuItem.getContentDataID());
    // FragmentTransaction transaction = getFragmentManager()
    // .beginTransaction();
    // transaction.replace(R.id.realtabcontent, genericFragment);
    // transaction.commit();
    // return true;
    // }
    // };

    @Override
    protected void initViewProperties() {
        if (this.mTabhostFormat.getBackgroundColor() != null) {
            this.mFragmentTabHost.setBackgroundResource(this.mTabhostFormat
                    .getBackgroundColorId());
        }
        for (int i = 0; i < this.mTabhostFormat.getPageCollection().size(); i++) {
            final PageItem tabItem = this.mTabhostFormat.getPageCollection()
                    .get(i);
            final String title = tabItem.getTitle();
            final TabSpec tabSpec = this.mFragmentTabHost.newTabSpec(tabItem
                    .getName());
            if (this.mTabhostFormat.getDisplay().equals(TabhostDisplay.Title)) {
                tabSpec.setIndicator(title, null);
            } else if (this.mTabhostFormat.getDisplay().equals(
                    TabhostDisplay.Icon)) {
                final Drawable drawable = getResources().getDrawable(
                        tabItem.getIconID());
                tabSpec.setIndicator("", drawable);
            } else if (this.mTabhostFormat.getDisplay().equals(
                    TabhostDisplay.TitleAndIcon)) {
                final LinearLayout view = (LinearLayout) LayoutInflater.from(
                        getApplicationContext())
                        .inflate(R.layout.aplink__generic_tabhost_indicator,
                                null, false);
                final TextView textView = (TextView) view
                        .findViewById(android.R.id.title);
                final ImageView imageView = (ImageView) view
                        .findViewById(android.R.id.icon);
                final Drawable drawable = getResources().getDrawable(
                        tabItem.getIconID());
                imageView.setImageDrawable(drawable);
                textView.setText(title);
                tabSpec.setIndicator(view);
            }
            final Bundle bundle = new Bundle();
            bundle.putSerializable(
                    GenericFragment.FORM_DESIGN,
                    FormDesignParser.getInstance().parse(
                            tabItem.getFormDesignID()));
            bundle.putSerializable(
                    GenericFragment.CONTENT_DATA,
                    ContentDataParser.getInstance().parseIntoObjProp(
                            tabItem.getContentDataID()));
            this.mFragmentTabHost
                    .addTab(tabSpec, GenericFragment.class, bundle);
            final View anchorView = this.mFragmentTabHost.getTabWidget()
                    .getChildAt(i);
            if (tabItem.getPopupMenuSpec() != null) {
                final ApLinkPopupMenu customPopupMenu = new ApLinkPopupMenu(
                        getGenericFragment(), tabItem.getPopupMenuSpec(),
                        anchorView);
                customPopupMenu.initView();
            }
        }
        this.mFragmentTabHost.setCurrentTab(this.mTabhostFormat.getStartTab());
    }

    @Override
    protected void loadBindingObject() {

    }

    @Override
    protected void loadDefaultObject() {

    }

    @Override
    protected void setEventListener() {

    }

    @Override
    public void setObjPropInfoTemp(final OnGetObjProInfo getObjProInfo) {
        throw new UnsupportedOperationException(this.getClass().getSimpleName()
                + " is not support this method");
    }

    @Override
    public void setOnClickListener(final OnClickListener onClickListener) {
        throw new UnsupportedOperationException(this.getClass().getSimpleName()
                + " is not support this method");
    }
}
